package cn.com.goldhs.architecture.ui;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class EditNameDialog {

	// resource
	private static ResourceBundle res = ResourceBundle.getBundle("architecture");
	
	// shell
	private Shell shell = null;
	
	// control variable
	private Button okButton = null;
	private Button cancelButton = null;
	
	//other variable
	private String value = null;
	private int userSelect;
	
	//constants
	
	
	public EditNameDialog(Shell shell) {
		shell = new Shell(shell, SWT.SHELL_TRIM | SWT.PRIMARY_MODAL);
		shell.setLayout(new GridLayout());
	}
	
	public int open() {
		createTextWidgets();
		createControlButtons();
		
		shell.pack();
		shell.open();
		
		Display display = shell.getDisplay();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		return userSelect;
	}


	private void createControlButtons() {
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		composite.setLayout(new GridLayout(2, true));
		
		okButton = new Button(composite, SWT.PUSH);
		okButton.setText(res.getString("Window.Dialog.OK"));
		okButton.setEnabled(false); // default false
		okButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				userSelect = SWT.OK;
				shell.close();
			}
		});
		
		cancelButton = new Button(composite, SWT.PUSH);
		cancelButton.setText(res.getString("Window.Dialog.Cancel"));
		cancelButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				userSelect = SWT.CANCEL;
				value = null;
				shell.close();
			}
		});
		
		shell.setDefaultButton(okButton);
		
	}

	private void createTextWidgets() {
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		composite.setLayout(new GridLayout(2, false));
		
		Label label = new Label(composite, SWT.RIGHT);
		label.setText(res.getString("Window.Dialog.Name"));
		
		final Text nameText = new Text(composite, SWT.BORDER);
		nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		if(value != null) 
			nameText.setText(value);
		nameText.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				value = nameText.getText().trim();
				
				okButton.setEnabled(value.length() != 0);
					
			}
		});
		
	}
	
	
	
}
